import Page from "@/src/components/page";
import PageWarpper, { PageWarpperContext } from "@/src/components/page-warpper";
import { State } from "@/src/dva/state";
import Api from "@/src/lib/api/api";
import Global from "@/src/lib/global";
import { View, Image, Text } from "@tarojs/components";
import { Component, Fragment } from "react";
import { connect } from "react-redux";
import Taro from '@tarojs/taro'
import  Event   from '@/src/dva/event';
// 创建事件总线
const eventBus = new Event ();


import "./index.scss"
// import ScrollView from "@/src/components/scroll-view";

interface getCouponState {
    data: any,
    id: any | string | number,
    isClick:boolean,
    isShow:boolean
}
interface getCouponProps {
    user: any
}


class GetCoupon extends Component<getCouponProps, getCouponState> {
    static contextType = PageWarpperContext
    constructor(props) {
        super(props)
        this.state = {
            data: {
                coupon_price: 5
            },
            id: '',
            isClick:false,
            isShow:false
        }
    }
    onLoad() {
        // console.log('哈哈哈哈哈')
        this.getData()
        // eventBus.on('sayHello',this.logins)

    }
    getData = () => {
        Taro.login().then(ret => {
            if (ret.code) {
                Api.getCheck({ code: ret.code }).then(res => {
                    console.log(res)
                    if (res?.data.isgetnewusercoupon && res?.data.isgetnewusercoupon == 1) {
                        // let data = {
                        //     coupon_price: 5
                        // }
                        this.setState({
                            isShow:false
                        })
                        setTimeout(() => {
                            Taro.reLaunch({
                                url: '/pages/home/index'
                            })
                        }, 0)
                       
                        // Taro.showToast({
                        //     title: '已领取过该优惠券',
                        //     icon: 'none',
                        //     duration: 2000
                        // })
                        
                        return
                    }else if(res?.data.isgetnewusercoupon && res?.data.isgetnewusercoupon == 0 && !res.data.coupon_id){
                        // Taro.showToast({
                        //     title: '暂无该优惠券',
                        //     icon: 'none',
                        //     duration: 2000
                        // })
                        // setTimeout(() => {
                        //     Taro.reLaunch({
                        //         url: '/pages/home/index'
                        //     })
                        // }, 2000)
                        this.setState({
                            isShow:false
                        })
                        return
                    }
                    this.setState({
                        data: res?.data,
                        id: res?.data.coupon_id,
                        isClick:true,
                        isShow:true
                    })
                })
            }
        })

    }
    isLogin = () => {
        const user = this.props.user;
        console.log(user);
        if (user.isLogin) {
            return false
        }

        const { loginCompentRef } = this.context
        loginCompentRef.current.show();
        return true;
    }
    // 重新执行事件
    logins=()=>{
        let id = this.state.id
        Api.goodsCouponGet({ id }).then(res => {
            console.log(res)
            let msg = '领券成功'
            if(res?.issuccess == 0 ){
                msg =res?.msg
            }
            Taro.showToast({
                title: msg,
                icon: 'none',
                duration: 1000
            })
            setTimeout(() => {
                Taro.reLaunch({
                    url: '/pages/home/index'
                })
            }, 1000)

        })
    }
    draw = () => {
       
        if (this.isLogin()) {
            eventBus.on('sayHello',this.logins)
            return
        }
        if(!this.state.isClick){
            return
        }
        this.logins()   
    }
    render() {
        const data = this.state.data
        const isShow = this.state.isShow;
        return (
            <Fragment>
                <Page navigationBar={{ title: "新用户专享" }} >
                    {
                        !isShow?(
                        <View className="isShowImga">
                        <Image src="http://r.punkyangsheng.com/xcx/img/images/jtyjs1.png" className="images" show-menu-by-longpress="true" mode="aspectFit"></Image>
                    </View>):(  <View onClick={this.isLogin} className="pagesBg">
                        <View className="pageHanderImg" >
                            <Image src={Global.loadImage('nes-3.png')} className="images"></Image>
                        </View>
                        <View className="kongs" style="width:100%;height:1px"></View>
                        <View className="prics">
                            <Text className="picerText">{parseInt(data.coupon_price)}</Text>
                            <Text className="picerText1">元</Text>
                        </View>
                        <View className="lingINg" onClick={this.draw}>
                            <Image className="lingq" src={Global.loadImage('new-2.png')}></Image>
                        </View>
                    </View>)
                    }
                  
                </Page>
            </Fragment>

        )
    }
}



export default connect((state: State) => ({ user: state.user }), (dispatch) => ({ dispatch }))(PageWarpper()(GetCoupon))
